/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Principal.java
 *
 * Created on 27-ene-2013, 15:07:19
 */
package edu.unl.asd.vistas;

import edu.unl.asd.controlador.AnalizaSintaxis;
import edu.unl.asd.tablas.ErrorColumnaModeloTabla;
import edu.unl.asd.tablas.ErrorModeloTabla;
import edu.unl.asd.vista.ayuda.Ayuda;
import java.awt.event.WindowAdapter;
import java.util.List;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 */
public class Principal extends javax.swing.JFrame {

    /** Creates new form Principal */
    public Principal() {
        initComponents();
        ErrorColumnaModeloTabla errorColumnaModeloTabla = new ErrorColumnaModeloTabla();
        errorColumnaModeloTabla.ancho_columnas(tabla_ManejoErrores);
        errorColumnaModeloTabla.disenio_cabecera(tabla_ManejoErrores);
        this.setLocationRelativeTo(null);
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(java.awt.event.WindowEvent evt) {
                int confirmar = JOptionPane.showConfirmDialog(null, "Desea salir de la aplicación", "CONFIRMAR", JOptionPane.YES_OPTION);
                if (confirmar == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        pnl_principal = new javax.swing.JPanel();
        pnl_despl = new javax.swing.JScrollPane();
        txa_Entrada = new javax.swing.JTextArea();
        pnl_despl_salida = new javax.swing.JScrollPane();
        txaSalida = new javax.swing.JTextArea();
        pnl_DoblePestania = new javax.swing.JTabbedPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        txaTablaSintactica = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        tabla_ManejoErrores = new org.jdesktop.swingx.JXTable();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        btn_validar = new javax.swing.JButton();
        btn_nuevo = new javax.swing.JButton();
        barraMenus = new javax.swing.JMenuBar();
        menuInicio = new javax.swing.JMenu();
        item_guardar = new javax.swing.JMenuItem();
        item_abrir = new javax.swing.JMenuItem();
        item_nuevo = new javax.swing.JMenuItem();
        item_salir = new javax.swing.JMenuItem();
        menuAyuda = new javax.swing.JMenu();
        subMenuAcerca = new javax.swing.JMenuItem();

        jMenu3.setText("File");
        jMenuBar2.add(jMenu3);

        jMenu4.setText("Edit");
        jMenuBar2.add(jMenu4);

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Analizador Sintáctico Descendente");
        setResizable(false);

        pnl_principal.setBackground(new java.awt.Color(204, 204, 204));
        pnl_principal.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 204), 4, true));
        pnl_principal.setForeground(new java.awt.Color(0, 0, 255));
        pnl_principal.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        pnl_despl.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        pnl_despl.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        txa_Entrada.setColumns(20);
        txa_Entrada.setRows(5);
        txa_Entrada.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 153, 153), 4));
        txa_Entrada.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txa_EntradaCaretUpdate(evt);
            }
        });
        pnl_despl.setViewportView(txa_Entrada);

        pnl_principal.add(pnl_despl, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 240, 597, 100));

        pnl_despl_salida.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        pnl_despl_salida.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        txaSalida.setColumns(20);
        txaSalida.setEditable(false);
        txaSalida.setRows(5);
        txaSalida.setToolTipText("Ingrese la cadena que desea validar:");
        txaSalida.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 153, 153), 4));
        txaSalida.setEnabled(false);
        pnl_despl_salida.setViewportView(txaSalida);

        pnl_principal.add(pnl_despl_salida, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 370, 600, 100));

        pnl_DoblePestania.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 153, 153), 4));
        pnl_DoblePestania.setFont(new java.awt.Font("Arial", 1, 14));

        txaTablaSintactica.setColumns(20);
        txaTablaSintactica.setEditable(false);
        txaTablaSintactica.setFont(new java.awt.Font("SansSerif", 0, 16));
        txaTablaSintactica.setRows(5);
        txaTablaSintactica.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 255, 204), 3));
        jScrollPane2.setViewportView(txaTablaSintactica);

        pnl_DoblePestania.addTab("Tabla Sintáctica", jScrollPane2);

        ErrorModeloTabla errmt= new ErrorModeloTabla();
        tabla_ManejoErrores.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 255, 204), 3));
        tabla_ManejoErrores.setModel(errmt);
        tabla_ManejoErrores.setFont(new java.awt.Font("SansSerif", 0, 14));
        tabla_ManejoErrores.setRowHeight(25);
        jScrollPane3.setViewportView(tabla_ManejoErrores);

        pnl_DoblePestania.addTab("Manejo de Errores", jScrollPane3);

        pnl_principal.add(pnl_DoblePestania, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 500, 720, 160));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36));
        jLabel1.setForeground(new java.awt.Color(51, 51, 51));
        jLabel1.setText("ANALIZADOR SINTACTICO DESCENDENTE");
        pnl_principal.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 0, -1, -1));

        jTextArea1.setColumns(20);
        jTextArea1.setFont(new java.awt.Font("Monospaced", 1, 14));
        jTextArea1.setForeground(new java.awt.Color(0, 153, 153));
        jTextArea1.setRows(5);
        jTextArea1.setText("CASO PRACTICO:\nPara la demostracioón de un Analizador Sintáctico Descendente nos hemos planteado la siguiente gramática:\nS---> (X)\nX---> ZY\nY---> ;X|E        E corresponde a Epsilon\nZ---> a|S\n\nLa gramatica acepta las siguientes cadena de entrada\n(a), (a;(a)), (a;(a;(a)))\n\nPara mayor informacion visitar el Menu de Ayuda.");
        jTextArea1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 153, 153), 4));
        jTextArea1.setCaretColor(new java.awt.Color(0, 102, 153));
        jTextArea1.setEnabled(false);
        jScrollPane1.setViewportView(jTextArea1);

        pnl_principal.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 40, 750, 170));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel2.setText("Tabla de Analizador Sintactico Descendente y Manejo de Errores");
        pnl_principal.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 480, -1, -1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel3.setText("Ingrese la cadena que desea validar:");
        pnl_principal.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 220, -1, -1));

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel4.setText("Indica un mensaje si la cadena ha sido validada o no");
        pnl_principal.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 350, -1, -1));

        btn_validar.setFont(new java.awt.Font("Arial", 0, 14));
        btn_validar.setText("Validar Sintaxis");
        btn_validar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_validarActionPerformed(evt);
            }
        });
        pnl_principal.add(btn_validar, new org.netbeans.lib.awtextra.AbsoluteConstraints(640, 240, -1, 40));

        btn_nuevo.setFont(new java.awt.Font("Arial", 0, 14));
        btn_nuevo.setText("Nuevo");
        btn_nuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_nuevoActionPerformed(evt);
            }
        });
        pnl_principal.add(btn_nuevo, new org.netbeans.lib.awtextra.AbsoluteConstraints(640, 290, 130, 40));

        barraMenus.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 51, 255), 3));
        barraMenus.setForeground(new java.awt.Color(0, 51, 153));

        menuInicio.setText("Inicio");
        menuInicio.setFont(new java.awt.Font("Arial", 1, 16));

        item_guardar.setFont(new java.awt.Font("Arial", 0, 14));
        item_guardar.setText("Guardar");
        item_guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                item_guardarActionPerformed(evt);
            }
        });
        menuInicio.add(item_guardar);

        item_abrir.setFont(new java.awt.Font("Arial", 0, 14));
        item_abrir.setText("Abrir");
        item_abrir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                item_abrirActionPerformed(evt);
            }
        });
        menuInicio.add(item_abrir);

        item_nuevo.setFont(new java.awt.Font("Arial", 0, 14));
        item_nuevo.setText("Nuevo");
        item_nuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                item_nuevoActionPerformed(evt);
            }
        });
        menuInicio.add(item_nuevo);

        item_salir.setFont(new java.awt.Font("Arial", 0, 14));
        item_salir.setText("Salir");
        item_salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                item_salirActionPerformed(evt);
            }
        });
        menuInicio.add(item_salir);

        barraMenus.add(menuInicio);

        menuAyuda.setText("Ayuda");
        menuAyuda.setFont(new java.awt.Font("Arial", 1, 16));

        subMenuAcerca.setFont(new java.awt.Font("Arial", 0, 14));
        subMenuAcerca.setText("Ayuda Contenidos");
        subMenuAcerca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                subMenuAcercaActionPerformed(evt);
            }
        });
        menuAyuda.add(subMenuAcerca);

        barraMenus.add(menuAyuda);

        setJMenuBar(barraMenus);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnl_principal, javax.swing.GroupLayout.DEFAULT_SIZE, 813, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnl_principal, javax.swing.GroupLayout.PREFERRED_SIZE, 676, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void subMenuAcercaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_subMenuAcercaActionPerformed
    new Ayuda();
    // TODO add your handling code here:
}//GEN-LAST:event_subMenuAcercaActionPerformed

private void txa_EntradaCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txa_EntradaCaretUpdate
    btn_validarActionPerformed(null);
    // TODO add your handling code here:
}//GEN-LAST:event_txa_EntradaCaretUpdate

private void item_salirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_item_salirActionPerformed
    int confirmar = JOptionPane.showConfirmDialog(null, "Desea salir de la aplicación", "CONFIRMAR", JOptionPane.YES_OPTION);
    if (confirmar == JOptionPane.YES_OPTION) {
        System.exit(0);
    }
    // TODO add your handling code here:
}//GEN-LAST:event_item_salirActionPerformed

private void item_abrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_item_abrirActionPerformed
    AnalizaSintaxis analizaSintaxis = new AnalizaSintaxis(txaTablaSintactica);
    StringTokenizer token = new StringTokenizer(txa_Entrada.getText().toString());
    analizaSintaxis.abrir(txa_Entrada, token);// TODO add your handling code here:
}//GEN-LAST:event_item_abrirActionPerformed

private void item_guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_item_guardarActionPerformed
    AnalizaSintaxis analizaSintaxis = new AnalizaSintaxis(txaTablaSintactica);
    StringTokenizer token = new StringTokenizer(txa_Entrada.getText().toString());
    analizaSintaxis.guardar_archivo(token, txa_Entrada);
    // TODO add your handling code here:
}//GEN-LAST:event_item_guardarActionPerformed

private void item_nuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_item_nuevoActionPerformed
    btn_nuevoActionPerformed(evt);// TODO add your handling code here:
}//GEN-LAST:event_item_nuevoActionPerformed

private void btn_validarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_validarActionPerformed
    AnalizaSintaxis analizaSintaxis = new AnalizaSintaxis(txaTablaSintactica);
    analizaSintaxis.ingresarEntrada(txa_Entrada.getText(), txaSalida, tabla_ManejoErrores);
    analizaSintaxis.analisisSintaxis();// TODO add your handling code here:
}//GEN-LAST:event_btn_validarActionPerformed

private void btn_nuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_nuevoActionPerformed
    txaSalida.setText("");
    txa_Entrada.setText("");// TODO add your handling code here:
}//GEN-LAST:event_btn_nuevoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
//            // Set System L&F
            UIManager.setLookAndFeel(
                    UIManager.getSystemLookAndFeelClassName());
        } catch (UnsupportedLookAndFeelException e) {
            // handle exception
        } catch (ClassNotFoundException e) {
            // handle exception
        } catch (InstantiationException e) {
            // handle exception
        } catch (IllegalAccessException e) {
//            // handle exception
        }
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
////        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Principal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuBar barraMenus;
    private javax.swing.JButton btn_nuevo;
    private javax.swing.JButton btn_validar;
    private javax.swing.JMenuItem item_abrir;
    private javax.swing.JMenuItem item_guardar;
    private javax.swing.JMenuItem item_nuevo;
    private javax.swing.JMenuItem item_salir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JMenu menuAyuda;
    private javax.swing.JMenu menuInicio;
    private javax.swing.JTabbedPane pnl_DoblePestania;
    private javax.swing.JScrollPane pnl_despl;
    private javax.swing.JScrollPane pnl_despl_salida;
    private javax.swing.JPanel pnl_principal;
    private javax.swing.JMenuItem subMenuAcerca;
    private org.jdesktop.swingx.JXTable tabla_ManejoErrores;
    private javax.swing.JTextArea txaSalida;
    private javax.swing.JTextArea txaTablaSintactica;
    private javax.swing.JTextArea txa_Entrada;
    // End of variables declaration//GEN-END:variables
    List<Error> errors;

    public List<Error> getErrors() {
        return errors;
    }

    public void setErrors(List<Error> errors) {
        this.errors = errors;
    }
}
